<template>
  <div class="page">
    <el-card>
      <el-row class="filter-container">
        <el-select v-model="query.zclb" style="width: 100px;" clearable
                   @change="handleMcChange2" class="filter-item" placeholder="资产类别"
        >
          <el-option
            v-for="item in zcDic"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          />
        </el-select>
        <el-select v-model="query.mc" style="width: 100px;" clearable
                   class="filter-item" placeholder="名称"
        >
          <el-option
            v-for="item in zclbDic2"
            :key="item.key"
            :label="item.label"
            :value="item.key"
          />
        </el-select>
        <el-button
          type="primary"
          size="small"
          class="filter-item"
          icon="el-icon-search"
          style="margin-left: 10px;"
          @click="handleSearch"
        >
          查询
        </el-button>
        <el-button v-if="role != 'zhixing'"
                   type="primary"
                   size="small"
                   class="filter-item"
                   icon="el-icon-plus"
                   @click="handleAdd"
        >
          新增
        </el-button>
      </el-row>
      <el-row>
        <el-table
          :data="tableData"
          border
          highlight-current-row
          fit
          :height="maxHeight"
          style="width: 100%"
          header-cell-class-name="table-header-custom"
          v-loading="tableLoading"
          size="small"
          ref="table"
        >
          <el-table-column label="名称" prop="mc">
            <template slot-scope="{ row }">
              {{
                row.mc && zcDic.find(item => item.code === row.zclb).dicDetails
                  .find(item => item.key === row.mc).label
              }}
            </template>
          </el-table-column>
          <el-table-column label="资产类别" prop="zclb">
            <template slot-scope="{ row }">
              {{ row.zclb && zcDic.find(item => item.code === row.zclb).name }}
            </template>
          </el-table-column>
          <el-table-column label="品牌" prop="pp" />
          <el-table-column label="规格型号" prop="ggxh" />
          <el-table-column label="出厂日期" prop="ccrq" />
          <el-table-column label="购买日期" prop="gmrq" />
          <el-table-column label="备注" prop="bz" show-overflow-tooltip />
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="scope">
              <el-button v-if="role != 'zhixing'" type="primary" size="mini"
                         @click="handleEdit(scope.row)"
              >
                编辑
              </el-button>
              <el-button v-if="role != 'caigou'" type="danger" size="mini"
                         @click="handleUpdateStatus(scope.row)"
              >
                报废
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.page"
          :page-size="page.pageSize"
          :page-sizes="page.pageSizes"
          :total="page.total"
          background
          layout="total, sizes, prev, pager, next, jumper, slot"
        >
          <el-button size="mini" icon="el-icon-refresh" @click="refresh" plain>刷新</el-button>
        </el-pagination>
      </el-row>
    </el-card>

    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      width="60%"
    >
      <el-form
        :model="form"
        ref="form"
        :rules="rules"
        size="small"
        :label-width="labelWidth"
        style="width: 100%;"
      >
        <el-row :gutter="40">
          <el-col :span="12">
            <el-form-item label="资产类别" prop="zclb">
              <el-select v-model="form.zclb" style="width: 100%;"
                         @change="handleMcChange"
              >
                <el-option
                  v-for="item in zcDic"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="mc">
              <el-select v-model="form.mc" style="width: 100%;">
                <el-option
                  v-for="item in zclbDic"
                  :key="item.key"
                  :label="item.label"
                  :value="item.key"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="40">
          <el-col :span="12">
            <el-form-item label="品牌" prop="pp">
              <el-input type="string" v-model="form.pp" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="ggxh">
              <el-input type="string" v-model="form.ggxh" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="40">
          <el-col :span="12">
            <el-form-item label="出厂日期" prop="ccrq">
              <el-date-picker
                v-model="form.ccrq"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                style="width: 100%;"
                :picker-options="pickerOptions"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="购买日期" prop="gmrq">
              <el-date-picker
                v-model="form.gmrq"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                style="width: 100%;"
                :picker-options="pickerOptions"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="40">
          <el-col :span="24">
            <el-form-item label="备注" prop="bz">
              <el-input type="textarea" :rows="5" v-model="form.bz" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import mixins from '@/utils/mixins/commonMixin';
import { save, getList, remove } from '@/api/assets-gl';
import { getList as getDicList } from '@/api/dic';

export default {
  name: 'AssetsGl',
  mixins: [mixins],
  data() {
    return {
      query: {
        zclb: '',
        mc: '',
      },
      tableData: [],
      page: {
        page: 1,
        pageSize: 10,
        total: 0,
        pageSizes: [10, 20, 30, 40, 50],
      },
      tableLoading: false,
      otherHeight: 125,
      form: {
        id: null,
        mc: '',
        zclb: '',
        pp: '',
        ggxh: '',
        ccrq: '',
        gmrq: '',
        bz: '',
        zt: '1',
        ztrq: '',
      },
      rules: {
        mc: [{ required: true, message: '不能为空', trigger: 'change' }],
        zclb: [{ required: true, message: '不能为空', trigger: 'change' }],
        pp: [{ required: true, message: '不能为空', trigger: 'blur' }],
        ggxh: [{ required: true, message: '不能为空', trigger: 'blur' }],
        ccrq: [{ required: true, message: '不能为空', trigger: 'change' }],
        gmrq: [{ required: true, message: '不能为空', trigger: 'change' }],
      },
      dialogFormVisible: false,
      labelWidth: '100px',
      op: 'create',
      title: '新建',
      zcDic: [],
      zclbDic: [],
      zclbDic2: [],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
    };
  },
  computed: {
    role() {
      if (this.$store.state.user.role) {
        return this.$store.state.user.role.roleCode;
      }
      return '';
    },
  },
  async mounted() {
    const zcDicRes = await this.getZcDic();
    this.zcDic = zcDicRes.data;
    this.handleSearch();
  },
  methods: {
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.page.page = val;
      this.getTableData();
    },
    getTableData() {
      const params = {
        page: this.page.page,
        pageSize: this.page.pageSize,
        ...this.query,
      };
      this.tableLoading = true;
      getList(params).then(res => {
        this.tableLoading = false;
        if (res.code === 200) {
          this.tableData = res.data;
          this.page.total = res.total;
        } else {
          this.tableData = [];
          this.page.total = 0;
        }
      });
    },
    handleSearch() {
      this.page.page = 1;
      this.getTableData();
    },
    handleAdd() {
      this.resetForm();
      this.op = 'create';
      this.title = '新建';
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    handleEdit(val) {
      this.resetForm();
      this.handleMcChange(val.zclb);
      Object.keys(this.form).forEach(item => {
        this.form[item] = val[item];
      });

      this.op = 'update';
      this.title = '编辑';
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    refresh() {
      this.getTableData();
    },
    resetForm() {
      this.form = {
        id: null,
        mc: '',
        zclb: '',
        pp: '',
        ggxh: '',
        ccrq: '',
        gmrq: '',
        bz: '',
        zt: '1',
        ztrq: '',
      };
    },
    handleSave() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          save(this.form).then(res => {
            if (res.code === 200) {
              this.$message.success(res.message);
              this.dialogFormVisible = false;
              this.refresh();
            } else {
              this.$message.error(res.message);
            }
          });
        }
      });
    },
    handleRemove(row) {
      remove({ id: row.id }).then(res => {
        if (res.code === 200) {
          this.$message.success(res.message);
          this.refresh();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    handleUpdateStatus(row) {
      const params = {
        id: row.id,
        zt: '2',
        ztrq: this.$dayjs().format('YYYY-MM-DD HH:mm:ss'),
      };
      save(params).then(res => {
        if (res.code === 200) {
          this.$message.success('操作成功');
          this.refresh();
        } else {
          this.$message.error('操作失败');
        }
      });
    },
    getZcDic() {
      const params = {
        page: 1,
        pageSize: 10000,
        description: '资产列表',
      };
      return getDicList(params);
    },
    handleMcChange(val) {
      this.form.mc = '';
      this.zclbDic = this.zcDic.find(item => item.code === val).dicDetails;
    },
    handleMcChange2(val) {
      this.query.mc = '';
      this.zclbDic2 = this.zcDic.find(item => item.code === val).dicDetails;
    },
  },
};
</script>

<style lang="scss" scoped>
.page {
  height: 100%;
  .el-card {
    height: 100%;
  }
}
</style>
